package com.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.activity.login.LoginActivity;
import com.alibaba.fastjson.JSONObject;
import com.example.login.R;
import com.tools.Address;
import com.tools.CommonDialog;
import com.tools.CommonThings;
import com.tools.OkHttp3;

import java.io.IOException;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

public class CenterFragment extends Fragment{
    private Button logout;
    //用户名，活动，卡券，积分，已预约，<dialog 总资产，活期，存款，理财 />
    private TextView username, activity, coupons, integral, appointment, budget, current, deposit, financial;
    private TextView welcome;
    private ImageView visMoney;

    private Boolean isVis = false; //dialog中数据默认不可见

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0x001) {
            }
            return false;
        }
    });

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_center, container,false);
        logout = view.findViewById(R.id.center_logout);
        username = view.findViewById(R.id.center_username);
        welcome = view.findViewById(R.id.center_welcome);
        activity = view.findViewById(R.id.center_activity);
        coupons = view.findViewById(R.id.center_card_coupons);
        integral = view.findViewById(R.id.center_integral);
        appointment = view.findViewById(R.id.center_appointment);
        visMoney = view.findViewById(R.id.center_dialog_visible);
        budget = view.findViewById(R.id.center_dialog_budget);
        current = view.findViewById(R.id.center_dialog_current);
        deposit = view.findViewById(R.id.center_dialog_deposit);
        financial = view.findViewById(R.id.center_dialog_financial);

        appointment.setTransformationMethod(PasswordTransformationMethod.getInstance());
        budget.setTransformationMethod(PasswordTransformationMethod.getInstance());
        current.setTransformationMethod(PasswordTransformationMethod.getInstance());
        deposit.setTransformationMethod(PasswordTransformationMethod.getInstance());
        financial.setTransformationMethod(PasswordTransformationMethod.getInstance());
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        String state = Objects.requireNonNull(getActivity()).getIntent().getStringExtra("state");

        if (("1").equals(state)){
            visMoney.setEnabled(true);
            username.setText("admin");
            welcome.setVisibility(View.VISIBLE);
            appointment.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }else {
            visMoney.setEnabled(false);
            username.setText("登录/开通");
            welcome.setVisibility(View.GONE);
        }

        username.setOnClickListener(view ->{
            if (!username.getText().toString().equals("admin")){
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });

        visMoney.setOnClickListener(view -> {
            if (!isVis){
                isVis = true;
                visMoney.setImageResource(R.drawable.pwd_vis);
                budget.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                current.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                deposit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                financial.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                isVis = false;
                visMoney.setImageResource(R.drawable.pwd_invis);
                budget.setTransformationMethod(PasswordTransformationMethod.getInstance());
                current.setTransformationMethod(PasswordTransformationMethod.getInstance());
                deposit.setTransformationMethod(PasswordTransformationMethod.getInstance());
                financial.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });

        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                initDialog();
            }
        });
    }

    private void getUserInfo(){
        RequestBody body = RequestBody.create(MediaType.parse("text/plain; charset=utf-8"), CommonThings.userId);
        OkHttp3.sendOkHttpPostTokenRequest(Address.getUserInfo(), body, CommonThings.tokenValue, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Objects.requireNonNull(getActivity()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getContext(),"服务器连接失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                JSONObject dataJSON = JSONObject.parseObject(res);
                Log.d("dataJSON", dataJSON.toString());
                if (dataJSON.getString("code").charAt(0) == '2'){
                    Toast.makeText(getContext(), "success", Toast.LENGTH_SHORT).show();
                }else if(dataJSON.getString("code").charAt(0) == '5'){
                    Objects.requireNonNull(getActivity()).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getContext(), dataJSON.getString("message"), Toast.LENGTH_SHORT).show();
                        }
                    });
                }else {
                    Objects.requireNonNull(getActivity()).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getContext(), "连接出错了", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }

    private void initDialog(){
        CommonDialog dialog = new CommonDialog(getContext());
        dialog.setTitle("退出提醒");
        dialog.setMessage("是否退出登录并退出程序");
        dialog.setOnClickBottomListener(new CommonDialog.OnClickBottomListener() {
            @Override
            public void onPositiveClick() {
                System.exit(0);
            }

            @Override
            public void onNegativeClick() {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
